package org.feidao.chapter82.controller;

import lombok.extern.slf4j.Slf4j;
import org.feidao.chapter82.common.serializeUtil;
import org.feidao.chapter82.model.Heroic;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * Created by linghufeixia on 2022-11-19.
 *
 */
@Slf4j
@RestController
@RequestMapping(value = "protoStuff")
public class ProtobufController {

    @RequestMapping(value = "/saveHeroic", method = RequestMethod.GET)
    public String saveHeroic(@RequestParam(value = "id") String id,
                             @RequestParam(value = "name") String name,
                           @RequestParam(value = "remarks") String remarks){
        Heroic heroic = new Heroic();
        heroic.setId(id);
        heroic.setName(name);
        heroic.setRemarks(remarks);

        //进行序列化
        byte[] bytes =  serializeUtil.serializeObjToByte(heroic);
        if(bytes==null){
            return "fail";
        }
        log.info("序列化后：" + Arrays.toString(bytes));

        //进行反序列化
        Heroic serializeHeroic =  serializeUtil.deserializeByteToObj(bytes);
        log.info("反序列化后：" + serializeHeroic.toString());

        return "success";
    }
}
